<template>
  <div>
    <el-card>
      <el-row>
        <el-col align="right">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="关键字">
              <el-input clearable placeholder="请输入公司名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button v-permission="['Admin']" icon="el-icon-search" type="primary">
                查询
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button v-permission="['Admin']" @click="dialogFormVisible = true;isbianji=true"
                         icon="el-icon-circle-plus-outline"
                         type="success">
                新增分公司
              </el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-table
          :data="list"
          style="width: 100%">
        <el-table-column
            prop="name"
            label="name"
            align="center">
        </el-table-column>
        <el-table-column
            prop="address"
            label="地址"
            align="center">
        </el-table-column>
        <el-table-column
            prop="telephone"
            label="联系电话"
            align="center">
        </el-table-column>
        <el-table-column
            align="center"
            label="操作">
          <template slot-scope="{ row }">
            <el-button v-permission="['Admin']" @click="bianji(row)" type="primary" size="mini">
              编辑
            </el-button>
            <el-button v-permission="['Admin']" @click="del(row.id)" type="danger" size="mini">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col align="right">
        <el-row style="margin:15px">
          <el-pagination
              background
              @current-change="currentChange"
              :current-page="page"
              :page-size="10"
              layout="total,  prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </el-row>
      </el-col>
    </el-card>
    <el-dialog @close="close"
               :visible.sync="dialogFormVisible">
      <el-form
          ref="form"
          :model="form"
          :rules="rules">
        <el-form-item label="公司名称" label-width="100px" prop="companyId">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" label-width="100px" prop="companyId">
          <el-input v-model="form.telephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" label-width="100px" prop="companyId">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addgongsi, gongsi, delgongsi, getCompany, modifygongsi} from "../../api";
import {deepClone, xx} from "../../uilts";

const getform = () => ({
  telephone: '',
  address: '',
  name: ''
})
export default {
  name: 'gongsi',
  data() {
    return {
      list: [],
      form: getform(),
      dialogFormVisible: false,
      isbianji: false,
      company: [],
      page: 1, //当前页码
      total: 0,//总条数
      rules:{
        telephone:xx(),
        address:xx(),
        name:xx(),
      }
    }
  },

  created() {
    this.getList()
    this.getCompany()
  },
  methods: {
    async getList() {
      const res = await gongsi({
        page: this.page,
        limit: 9,
      })
      this.list = res.data.data.list
      this.page = res.data.data.pageNum
      this.total = res.data.data.total
    },
    //获取分公司
    async getCompany() {
      const res = await getCompany({
        limit: 100
      })
      this.company = res.data.data.list
    },
    //翻页
    currentChange(pagee) {
      this.page = pagee
      this.getList()
    },
    //新增and修改
    bianji(row) {
      this.isbianji = false
      this.dialogFormVisible = true
      this.form = deepClone(row)
    },
    submit() {
      console.log(this.$refs.form)
      this.$refs.form.validate(async (res) => {
        if (res) {
          if (this.isbianji) {
            await addgongsi(this.form)
          } else {
            await modifygongsi(this.form)
          }
          this.$message.success('操作成功！')
          this.dialogFormVisible = false
          await this.getList()
        }
      })
    },

    //删除
    delgongsi(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delgongsi(id)
        this.$message.success('删除成功！')
        await this.getList()

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    close() {
      this.form = getform()
      this.$refs.form.resetFields()
    }

    //查
  }
}
</script>
<style scoped>

</style>






